package com.lu.sb211201rocketmq.controller;


import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.client.exception.MQBrokerException;
import org.apache.rocketmq.client.exception.MQClientException;
import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.common.message.MessageExt;
import org.apache.rocketmq.remoting.exception.RemotingException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.nio.charset.StandardCharsets;
import java.util.Date;

/**
 * @author Administrator
 */
@Slf4j
@RestController
@RequestMapping("producer")
public class ProducerController {


    @Autowired
    private DefaultMQProducer defaultMQProducer;

    @GetMapping("send")
    public void send() throws InterruptedException, RemotingException, MQClientException, MQBrokerException {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("test", new Date());
        MessageExt mqMessage = new MessageExt();
        mqMessage.setTopic("test-topic");
        mqMessage.setTags("test-tag");
        mqMessage.setBody(jsonObject.toString().getBytes(StandardCharsets.UTF_8));
        defaultMQProducer.send(mqMessage);
    }

}
